package com.jeeplus.modules.monitor.task;

import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.ap.entity.DayDetil;
import com.jeeplus.modules.ap.mapper.DayDetilMapper;
import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.entity.Project;
import com.jeeplus.modules.infor.mapper.GoodsMapper;
import com.jeeplus.modules.infor.mapper.ProjectMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.tms.entity.*;
import com.jeeplus.modules.tms.mapper.*;
import org.quartz.DisallowConcurrentExecution;

import com.jeeplus.modules.monitor.entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@DisallowConcurrentExecution  
public class TestTask1 extends Task{
	private static JdbcTemplate jdbc = SpringContextHolder.getBean(JdbcTemplate.class);
	private static ProjectMapper projectMapper = SpringContextHolder.getBean(ProjectMapper.class);
	private static UnissuedDetilMapper unissuedDetilMapper = SpringContextHolder.getBean(UnissuedDetilMapper.class);
	private static GoodsMapper goodsMapper = SpringContextHolder.getBean(GoodsMapper.class);
	private static UnissuedMapper unissuedMapper = SpringContextHolder.getBean(UnissuedMapper.class);
	private static TransportPlanningMapper transportPlanningMapper = SpringContextHolder.getBean(TransportPlanningMapper.class);
	private static MaterialInformationsMapper materialInformationsMapper = SpringContextHolder.getBean(MaterialInformationsMapper.class);
	private static NewBillMapper billMapper = SpringContextHolder.getBean(NewBillMapper.class);
	private static TmsMaterialsInformationMapper tmsMaterialsInformationMapper = SpringContextHolder.getBean(TmsMaterialsInformationMapper.class);

	@Override
	public void run() {
		NumberFormat nf = NumberFormat.getInstance();
		SimpleDateFormat s= new SimpleDateFormat("YYYY-MM-dd");
		List<Project> list=projectMapper.findList(new Project());
		for(Project p:list){
			Unissued u=new Unissued();
			String sql="select * from tms_new_planning where pro='"+p.getName()+"' and to_days(create_date) = to_days(now())";
			List<Map<String,Object>> lis=jdbc.queryForList(sql);
			u.setDate(s.format(new Date()));
			p=projectMapper.get(p.getId());
			u.setPro(p.getName());
			u.setId(UUID.randomUUID().toString().replace("-",""));
			unissuedMapper.insert(u);
			Double d=0.0;
			String  ids="";
			for(Map<String,Object> m:lis){
               d+=Double.parseDouble(m.get("weight")+"");
               ids+=m.get("id")+",";
			}
			u.setDtbz(nf.format(d));
			String sql1="select sum(weight) as sum from tms_new_planning where pro='"+p.getName()+"' and status='已创建'";
			String sql2="select sum(wf) as sum  from tms_new_transport where pro='"+p.getName()+"'";
			Map<String,Object> list1=jdbc.queryForMap(sql1);
			Map<String,Object> list2=jdbc.queryForMap(sql2);
			Object sum=list1.get("sum");
			Object sunm=list2.get("sum");
			if(sum==null){
				sum=0;
			}
			if(sunm==null){
				sunm=0;
			}
            double zwf=Double.parseDouble(sum+"")+Double.parseDouble(sunm+"");
		    u.setZwf(nf.format(zwf).replaceAll(",",""));
			//	u.setDtwf(nf.format(dd));
		//	u.setDtfy(nf.format(ddd));
			String sq="select * from tms_new_bill where pro='"+p.getName()+"' and to_days(create_date) = to_days(now())";
            List<Map<String,Object>> bills=jdbc.queryForList(sq);
            Double c=0.0;
            double cc=0.0;
            for(Map<String ,Object> bill:bills){
            	NewBill waybill=billMapper.get(bill.get("id")+"");
            	boolean b=false;
				c+=Double.parseDouble(waybill.getWeight());
            	if(ids.contains(bill.get("pid")+"")){
					cc+=Double.parseDouble(waybill.getWeight());
				}

			}
			u.setZfyl(nf.format(c).replaceAll(",",""));
            u.setDtfy(nf.format(cc).replaceAll(",",""));
            double zqwf=zwf+c-d;
            double dtwf=d-cc;
            u.setDtwf(nf.format(dtwf).replaceAll(",",""));
            u.setZqwf(nf.format(zqwf).replaceAll(",",""));
			unissuedMapper.update(u);
		}




	}

}
